﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.bullet 
{
	import com.sng.stg.game.event.PlayerHit;
	import com.sng.stg.game.Player.Player;
	import com.sng.stg.game.StageViewer;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author snega
	 */
	public class EnemyBullet extends Sprite
	{
		public var _stage:StageViewer = null;
		protected var _currBitmap:Bitmap = null;
		
		public var _previous:EnemyBullet = null;
		public var _next:EnemyBullet = null;

		public var _vx:Number = 0;
		public var _vy:Number = 0;
		public var _ax:Number = 0;
		public var _ay:Number = 0;
		
		public var _radius:Number = 1;
		public var _radiusSquare:int = 1;
		public var _radiusGrazeSquare:int = 4;
		
		public var _player:Player = null;
		
		public function EnemyBullet(stage:StageViewer, radius:Number = 1, bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false) 
		{
			this._currBitmap = new Bitmap(bitmapData, pixelSnapping, smoothing);
			this._currBitmap.x = - this._currBitmap.width/2;
			this._currBitmap.y = - this._currBitmap.height / 2;
			
			this._radius = radius;
			this._radiusSquare = radius * radius;
			this._radiusGrazeSquare = 2 * this._radiusSquare;
			
			this._stage = stage;
			if (stage != null) this._player = stage._player;

			this.addChild(this._currBitmap);
		}
		
		public function push(o:EnemyBullet):void {
			if (this._next == null) {
				o._previous = this;
				
				this._next = o;
			}
			else {
				this._next._previous = o;
				
				o._next = this._next;
				o._previous = this;
				
				this._next = o;
			}
		}
		
		public function remove():void {
			if (this._next != null) {
				this._next._previous = this._previous;
			}
			
			if (this._previous != null) {
				this._previous._next = this._next;
			}
		}
		
		public function start(stage:StageViewer):void {
			this._stage = stage;
			this._player = stage._player;
		}
		
		public function process():void {
			var ox:int = this.x - this._player.x;
			var oy:int = this.y - this._player.y;
			var n:int = ox * ox + oy * oy;
			if (n < this._radiusGrazeSquare) {
				this._player._point += 10;
				this._player._graze ++;
				
				if (n < this._radiusSquare) {
					trace("Player was hit!");
					this._player.hit();
					
					this._stage._2dElement.removeChild(this);
					this.remove();
				}
			}
			
			this.x += this._vx;
			this.y += this._vy;
			
			this._vx += this._ax;
			this._vy += this._ay;
		}
		
	}
	
}